package cn.luo.controller;

import cn.luo.entity.Tag;
import cn.luo.service.BlogsService;
import cn.luo.service.TagService;
import cn.luo.service.UserService;
import cn.luo.uilt.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/user")
public class UserControoler {
    @Autowired
    BlogsService blogsService;

    @Autowired
    UserService userService;

    @Autowired
    TagService tagService;

    //去个人中心
    @RequestMapping("/myhome")
    public String home(){
        return "user/myhome";
    }

    //跳转到富文本编辑器页面
    @RequestMapping("/compile")
    public String doCompile(){
        return "user/editor";
    }

    //跳转到文章管理
    @RequestMapping("/myblogs")
    public String myblogs(){
        return "user/myblogs";
    }

    //添加内容-写博文
    @RequestMapping("/addboke")
    @ResponseBody
    public void add(@RequestBody Map<String,Object> map){
        System.out.println("map = " + map);
        blogsService.addboke(map);
    }

    //分页查询
    @ResponseBody
    @RequestMapping("/findByPage2/{start}/{num}")
    public Map<String,Object> findByPage(@PathVariable("start")int start,
                                          @PathVariable("num")int num){
        System.out.println("start = " + start);
        System.out.println("num = " + num);
        Map<String,Object> map = new HashMap<>();
        map.put("blogs",userService.finfByPage2(start,num));
        map.put("total",userService.total());
        return map;
    }

    //查看全部类型
    @ResponseBody
    @RequestMapping("/tag/findAll")
    public List<Tag> findAll(){
        List<Tag> lst = tagService.findAll();
        return lst;
    }

    //添加类型
    @ResponseBody
    @RequestMapping("/tag/add")
    public Map<String, Object> addTag(@RequestBody Tag tag){
        tagService.addTag(tag);
        return RespJson.pack(200,"添加成功",null);
    }
}
